import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isFunction from "lodash/isFunction";
import _forEach from "lodash/forEach";

var _dec, _class;

/**
 * FieldProp.js
 * @date 2018-10-04
 */
import React from 'react';
import { Form, Input, Checkbox as CpaasCheckbox, Divider } from 'antd';
import Checkbox from 'components/Checkbox';
import intl from 'utils/intl';
import { attributeNameProp, attributeValueProp, fieldLabelProp, fieldNameProp } from "../../config";
import fieldComponents from "../FiledComponent/index"; // 当 组件是 Checkbox 或者 Switch 时, requiredFlag 为 0, 且不能编辑

var requiredFlagNoNeedStyle = {
  display: 'none'
};
var FieldProp = (_dec = Form.create({
  fieldNameProp: null,
  onValuesChange: function onValuesChange(props, changedValues, allValues) {
    if (_isFunction(props.onValuesChange)) {
      props.onValuesChange(props, changedValues, allValues);
    }
  }
}), _dec(_class = /*#__PURE__*/function (_React$Component) {
  _inherits(FieldProp, _React$Component);

  var _super = _createSuper(FieldProp);

  function FieldProp() {
    _classCallCheck(this, FieldProp);

    return _super.apply(this, arguments);
  }

  _createClass(FieldProp, [{
    key: "render",
    value: // state = {
    // };
    //
    // constructor(props) {
    //   super(props);
    // }
    function render() {
      var field = this.props.field;
      var propValues = {};

      _forEach(field.config, function (prop) {
        propValues[prop[attributeNameProp]] = prop[attributeValueProp];
      });

      return /*#__PURE__*/React.createElement(Form, null, this.renderFieldCommonProps(propValues), /*#__PURE__*/React.createElement(Divider, null), this.renderFieldTypeProps(propValues));
    }
  }, {
    key: "renderFieldCommonProps",
    value: function renderFieldCommonProps(propValues) {
      var _this$props = this.props,
          form = _this$props.form,
          field = _this$props.field;
      var isBooleanLikeValue = field.componentType === 'Checkbox' || field.componentType === 'Switch';
      var requiredFlagStyle = isBooleanLikeValue ? requiredFlagNoNeedStyle : undefined;
      var requiredFlag = isBooleanLikeValue ? 0 : field.requiredFlag === 1 ? 1 : 0;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, {
        key: "enabledFlag"
      }, form.getFieldDecorator('enabledFlag', {
        initialValue: field.enabledFlag === 0 ? 0 : 1
      })( /*#__PURE__*/React.createElement(CpaasCheckbox, {
        checkedValue: 0,
        unCheckedValue: 1
      }, "\u53EA\u8BFB"))), /*#__PURE__*/React.createElement(Form.Item, {
        key: "requiredFlag",
        style: requiredFlagStyle
      }, form.getFieldDecorator('requiredFlag', {
        initialValue: requiredFlag
      })( /*#__PURE__*/React.createElement(Checkbox, null, "\u5FC5\u8F93"))), /*#__PURE__*/React.createElement(Form.Item, {
        className: "ant-form-item-checkbox-cascade-parent",
        key: "labelDisplayFlag"
      }, form.getFieldDecorator('labelDisplayFlag', {
        initialValue: propValues.labelDisplayFlag !== false,
        valuePropName: 'checked'
      })( /*#__PURE__*/React.createElement(CpaasCheckbox, null, "\u663E\u793A\u6807\u9898"))), /*#__PURE__*/React.createElement(Form.Item, {
        className: "ant-form-item-checkbox-cascade-child",
        key: fieldLabelProp
      }, form.getFieldDecorator(fieldLabelProp, {
        initialValue: field[fieldLabelProp]
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(Form.Item, {
        label: "\u5B57\u6BB5\u540D",
        key: fieldNameProp
      }, form.getFieldDecorator(fieldNameProp, {
        initialValue: field[fieldNameProp],
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: '字段名'
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(Form.Item, {
        label: "\u63CF\u8FF0",
        key: "description"
      }, form.getFieldDecorator('description', {
        initialValue: propValues.description
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(Form.Item, {
        label: "\u4E3A\u7A7A\u63D0\u793A",
        key: "placeholder"
      }, form.getFieldDecorator('placeholder', {
        initialValue: propValues.placeholder
      })( /*#__PURE__*/React.createElement(Input, null))));
    }
  }, {
    key: "renderFieldTypeProps",
    value: function renderFieldTypeProps() {
      var propValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props2 = this.props,
          field = _this$props2.field,
          form = _this$props2.form;
      var FieldComponent = fieldComponents[field.componentType];

      if (FieldComponent) {
        return /*#__PURE__*/React.createElement(FieldComponent, {
          form: form,
          propValues: propValues
        });
      }

      return null;
    }
  }]);

  return FieldProp;
}(React.Component)) || _class);

if (process.env.NODE_ENV === 'production') {
  FieldProp.displayName = 'DynamicForm(FieldProp)';
}

export default FieldProp;